package src.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

import src.percolation.Red;

public class Utils {

public static double findPC(int large, long seed, double eps) {
		
	
		double proba = .5; //probalidad inicial		
				
		int n = 2; //contador
		double lastNotPercolating = 0; //valores iniciales
		double lastPercolating = 1;
		
		while(lastPercolating - lastNotPercolating > eps) {
		
			Red red = new Red(large,seed);//re creating the grid here, with the same seed
			red.createGrid(proba);
			
			if (red.doesPercolate()) {
				lastPercolating = proba;
				proba -= 1/Math.pow(2,n);
			}
			else {
				lastNotPercolating = proba;
				proba += 1/Math.pow(2,n);
			}
			n++;
		}
		return proba;
	}	
	
	public static void printHistogram(Map histogram) {
		
		Set values = histogram.keySet();
		
		for(Object value: values) {
			System.out.println(value+","+histogram.get(value));
		}
	}
	
	public static void printHistogramToFile(Map<Integer,Integer> histogram,String fileName) throws IOException {
		
		FileOutputStream fos = new FileOutputStream(new File(fileName));
	
		Set<Integer> values = histogram.keySet();
		
		for(Integer value: values) {
			fos.write((value+","+histogram.get(value)+"\n").getBytes());
		}
		fos.flush();
		fos.close();
	}
	
	public static void printToFile(Map data, String fileName) throws IOException {
		
		FileOutputStream fos = new FileOutputStream(new File(fileName));
		
		Set values = data.keySet();
		
		for(Object value : values) {
			fos.write((value+","+data.get(value)+"\n").getBytes());
		}
		
		fos.flush();
		fos.close();
	}
	
	public static double[][] mapToDoubleArray(Map map) {
		
		boolean isDouble = false;
		boolean isInteger = false;
		
		Object key = map.keySet().iterator().next();
		
		if(key.getClass().equals(Double.class)) {
			isDouble = true;
		}else if(key.getClass().equals(Integer.class)) {		
			isInteger = true;
		
		}

		double[][] array = new double[map.size()][2];
		
		if(isDouble) {
			
			Set<Double> keys = map.keySet();
			
			int i = 0;
			for (Double key1 : keys) {
				array[i][0] = key1.doubleValue();
				array[i][1] = ((Double)map.get(key1)).doubleValue();
				i++;
			}
			
			
		}
		
		if(isInteger) {
			
			Set<Integer> keys = map.keySet();
			
			int i = 0;
			for (Integer key1 : keys) {
				array[i][0] = key1.doubleValue();
				array[i][1] = ((Integer)map.get(key)).doubleValue();
				i++;
			}
		}		
	return array;
	}	
	
	public static double[][] logLogData(double[][] map){
		
		double[][] logarizedMap = new double[map.length][2];
		
		for(int i=0; i<map.length;i++) {
			logarizedMap[i][0] = Math.log(map[i][0]);
			logarizedMap[i][1] = Math.log(map[i][1]);
		}	
		return logarizedMap;
	}
	
	public static void printArray(double[][] array) {
		
		int height = array.length;
		int wide = array[0].length;
		
		for(int i = 0; i<height; i++){
			for(int j = 0; i<wide; j++){
				System.out.print(array[i][j]);
				System.out.print(",");
			}
			System.out.println("");
		}
	}
	
}	